package com.weiyi.weiman.api;

import com.weiyi.weiman.annotation.Log;
import com.weiyi.weiman.domain.dto.BrowseHistoryDTO;
import com.weiyi.weiman.domain.dto.CollectComicsDTO;
import com.weiyi.weiman.domain.dto.ResultInfo;
import com.weiyi.weiman.exception.APIException;
import com.weiyi.weiman.exception.TokenException;
import com.weiyi.weiman.service.user.UserComicsService;
import com.weiyi.weiman.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhang
 * @Date 2021/5/9 下午3:56
 * @Description 漫画指数接口
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserComicsController {

    @Autowired
    private UserComicsService userComicsService;

    @Log
    @PostMapping("/giveLike/{comicsId}")
    public ResultInfo giveLike(@PathVariable("comicsId") long comicsId,
                               HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long comicsLike = null;
        try {
            comicsLike = userComicsService.giveLike(comicsId, token);
        } catch (TokenException e) {
            return ResultUtils.failureInfo(e.getTokenStatus());
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(comicsLike);
    }

    /**
     * 取消漫画点赞
     * @param comicsId
     * @param request
     * @return
     */
    @Log
    @DeleteMapping("/giveLike/{comicsId}")
    public ResultInfo cancelLike(@PathVariable("comicsId") long comicsId,
                               HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long comicsLike = null;
        try {
            comicsLike = userComicsService.cancelLike(comicsId, token);
        } catch (TokenException e) {
            return ResultUtils.failureInfo(e.getTokenStatus());
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(comicsLike);
    }

    /**
     * 获取用户浏览记录列表
     * @param request
     * @return
     */
    @Log
    @GetMapping("/historyList")
    public ResultInfo getHistory(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        List<BrowseHistoryDTO> list = null;
        try {
            list = userComicsService.getHistory(token);
        } catch (TokenException e) {
            return ResultUtils.failureInfo(e.getTokenStatus());
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(list);
    }

    /**
     * 删除历史记录
     * @param historyId
     * @param request
     * @return
     */
    @Log
    @DeleteMapping("/history/{comicsId}")
    public ResultInfo deleteHistory(@PathVariable("comicsId") long comicsId,
                                    HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            userComicsService.deleteHistory(comicsId, token);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo("删除历史记录成功");
    }

    /**
     * 收藏漫画
     * @param comicsId
     * @param request
     * @return
     */
    @Log
    @PostMapping("/collect/{comicsId}")
    public ResultInfo collectComics(@PathVariable("comicsId") long comicsId,
                                    HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            userComicsService.collectComics(comicsId, token);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo("漫画收藏成功");
    }

    /**
     * 取消收藏漫画
     * @param comicsId
     * @param request
     * @return
     */
    @Log
    @DeleteMapping("/collect/{collectId}")
    public ResultInfo cancelCollect(@PathVariable("collectId") long comicsId,
                                    HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            userComicsService.cancelCollect(comicsId, token);
        } catch (TokenException e) {
            return ResultUtils.failureInfo(e.getTokenStatus());
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo("漫画取消收藏成功");
    }

    /**
     * 获取用户收藏漫画的列表
     * @param request
     * @return
     */
    @Log
    @GetMapping("/collectList")
    public ResultInfo getCollectList(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        List<CollectComicsDTO> list = null;
        try {
            list = userComicsService.getCollectList(token);
        } catch (TokenException e) {
            return ResultUtils.failureInfo(e.getTokenStatus());
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(list);
    }

}
